package com.yf1706.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.CountOvertimeSalaryRate;
import com.yf1706.service.CountOvertimeSalaryRateService;
import com.yf1706.tools.JsonTool;

/**
 * 
 * @author 锦涛
 *
 */
@Controller
@RequestMapping("OvertimeRate")
public class CountOvertimeSalaryRateController extends BaseController{

	@Autowired
	CountOvertimeSalaryRateService OvertimeRateService;
	
	@RequestMapping("querybypage")
	@ResponseBody
	public Map<String, Object> querybypage(int page,int rows,String sort,String order) {
		Page<CountOvertimeSalaryRate> pages = OvertimeRateService.querybypage(page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<CountOvertimeSalaryRate> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_OSR_rowData"));
		CountOvertimeSalaryRate OvertimeRate = new CountOvertimeSalaryRate();
		OvertimeRate.setEverydayWorktime(jObject.getIntValue("everydayWorktime"));
		OvertimeRate.setOvertimeSalaryRateName(jObject.getString("overtimeSalaryRateName"));
		JsonTool jTool = new JsonTool();
		try {
			OvertimeRateService.save(OvertimeRate);
			jTool.setSuccess(true);
			jTool.setMsg("添加【"+OvertimeRate.getOvertimeSalaryRateName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加【"+OvertimeRate.getOvertimeSalaryRateName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("update")
	@ResponseBody
	public void update(HttpServletRequest request,HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_OSR_rowData"));
		CountOvertimeSalaryRate OvertimeRate = new CountOvertimeSalaryRate();
		OvertimeRate.setOvertimeSalaryRateId(jObject.getIntValue("overtimeSalaryRateId"));
		OvertimeRate.setEverydayWorktime(jObject.getIntValue("everydayWorktime"));
		OvertimeRate.setOvertimeSalaryRateName(jObject.getString("overtimeSalaryRateName"));
		OvertimeRate.setRate(jObject.getDoubleValue("rate"));
		JsonTool jTool = new JsonTool();
		try {
			OvertimeRateService.update(OvertimeRate);
			jTool.setSuccess(true);
			jTool.setMsg("编辑【"+OvertimeRate.getOvertimeSalaryRateName()+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑【"+OvertimeRate.getOvertimeSalaryRateName()+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("delete")
	@ResponseBody
	public void delete(HttpServletRequest request,HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_OSR_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			OvertimeRateService.remove(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除级数为【"+ids+"】成功！");
		} catch (Exception e) {
			jTool.setMsg("删除级数为【"+ids+"】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}
}
